package trss.model.torrent;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import trss.bencode.BDictionary;
import trss.bencode.BElement;
import trss.bencode.BList;
import trss.bencode.Util;

@Entity
@Table(name = "metafile_info_file")
public class File {
    private Long length = null;
    private String md5sum = null;
    private List<String> path = new ArrayList<String>();
    private Long id;
    
    public File(){}
    /**
     * @param bElement
     */
    public File(BDictionary bElement) {
        length = Util.getLong(bElement.get("length"));
        md5sum = Util.getString(bElement.get("md5sum"));
        BList path = (BList) bElement.get("path");
        for (BElement pathElement : path) {
            this.path.add(Util.getString(pathElement));
        }
    }

    /**
     * @return the length
     */
    @Column
    public Long getLength() {
        return length;
    }

    /**
     * @param length
     *            the length to set
     */
    public void setLength(Long length) {
        this.length = length;
    }

    /**
     * @return the md5sum
     */
    @Column
//    @SearchableProperty
    public String getMd5sum() {
        return md5sum;
    }

    /**
     * @param md5sum
     *            the md5sum to set
     */
    public void setMd5sum(String md5sum) {
        this.md5sum = md5sum;
    }

    /**
     * @return the path
     */
//    @CollectionOfElements
    public List<String> getPath() {
        return path;
    }

    /**
     * @param path
     *            the path to set
     */
    public void setPath(List<String> path) {
        this.path = path;
    }

    /**
     * @return the id
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
//    @SearchableId
    public Long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * ${@inheritDoc}
     */
    @Override
    public String toString() {
        return "File [length=" + length + ", md5sum=" + md5sum + ", path="
                + path + "]";
    }

    /**
     * ${@inheritDoc}
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((length == null) ? 0 : length.hashCode());
        result = prime * result + ((md5sum == null) ? 0 : md5sum.hashCode());
        result = prime * result + ((path == null) ? 0 : path.hashCode());
        return result;
    }

    /**
     * ${@inheritDoc}
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        File other = (File) obj;
        if (length == null) {
            if (other.length != null) {
                return false;
            }
        } else if (!length.equals(other.length)) {
            return false;
        }
        if (md5sum == null) {
            if (other.md5sum != null) {
                return false;
            }
        } else if (!md5sum.equals(other.md5sum)) {
            return false;
        }
        if (path == null) {
            if (other.path != null) {
                return false;
            }
        } else if (!path.equals(other.path)) {
            return false;
        }
        return true;
    }

}
